<template>
  <span @click="load">
    <slot>上传 </slot>
    <input ref="input" type="file" style="display: none" @change="change" />
  </span>
</template>
<script setup>
import axios from 'axios'
import { ref } from 'vue'
import { message } from 'ant-design-vue'
const input = ref(null)
const props = defineProps({
  limit: {
    type: String,
    default: 'sheet'
  },
  url: {
    type: String,
    default: ''
  }
})
const emit = defineEmits(['success'])
const load = () => {
  input.value.click()
}
const change = e => {
  upload(e.target.files[0])
}
const upload = file => {
  console.log(file.type)
  const formData = new FormData()
  formData.append('file', file)
  axios({
    url: props.url
      ? process.env.VUE_APP_ERP_PROOF + props.url
      : process.env.VUE_APP_ERP_IMG,
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data',
      userId: localStorage.userId || 388,
      token: localStorage.userId || 388,
      Authorization: localStorage.token,
      tokenSign: 2,
      accountId: localStorage.accountId
    },
    data: formData
    //    withCredentials: true 指定域名
  }).then(res => {
    input.value.value = ''
    if (!res.data.success) {
      message.error(res.data.retInfo)
      return
    }
    if (res.data.success && !res.data.data) {
      message.success('success')
      emit('success')
    }
    if (res.data.success && res.data.data && res.data.data.errorMsg) {
      if (res.data.data.errorMsg === 'success') {
        message.success('success')
        emit('success')
      } else {
        message.warning(res.data.data.errorMsg)
        if (res.data.data.errorUrl) {
          let a = document.createElement('a')
          a.setAttribute('href', res.data.data.errorUrl)
          a.setAttribute('download', '')
          a.click()
          a = null
        }
      }
    }
  })
}
// createBaseImg(urlList[count.value])
</script>
<script>
//         function ajax(url) {
//             // 当请求执行完了之后，执行到then之后，又执行到了next();进行下一步请求
//             axios.get(url).then(res=>consrdata.next(res.data))
//         }
//         function* step() {
//             const users = yield ajax('https://api.github.com/users');
//             const fisterUser = yield
//                   ajax(`https://api.github.com/users/${users[0].login}`);
//             const flolowers = yield ajax(fisterUser.flolowers_url)
//         }
//         var consrdata=step();
//         consrdata.next();//开始执行第一步
//
</script>
